window.total = 0;
window.failed = 0;
window.$testGroup = undefined;
window.startTime = 0;
window.$header = $('<div class="header">Executed <span class="testCount">0</span> tests. FAILED (<span class="failedCount">0</span>) PASSED (<span class="passedCount">0</span>) in <span class="timeCount">0</span>ms</div>');

function startTestGroup(name) {
    if ($testGroup !== undefined) endTestGroup();
    $testGroup = $('<div class="test"><h1>' + name + '</h1></div>');
    $testGroup.append('Executed <span class="testCount">0</span> tests. FAILED (<span class="failedCount">0</span>) PASSED (<span class="passedCount">0</span>) in <span class="timeCount">0</span>ms<br /><br />');
    $('body').append($testGroup);
    total = 0; failed = 0; startTime = (new Date()).getTime();
}

function endTestGroup() {
    var endTime = (new Date()).getTime() - startTime;

    $(".testCount", $testGroup).html(total);
    $(".testCount", $header).html(parseInt($(".testCount", $header).html()) + total);

    $(".failedCount", $testGroup).html(failed);
    $(".failedCount", $header).html(parseInt($(".failedCount", $header).html()) + failed);

    $(".passedCount", $testGroup).html(total - failed);
    $(".passedCount", $header).html(parseInt($(".passedCount", $header).html()) + (total - failed));

    $(".timeCount", $testGroup).html(endTime);
    $(".timeCount", $header).html(parseInt($(".timeCount", $header).html()) + endTime);

    total = 0; failed = 0; startTime = 0; $testGroup = undefined;
}

function fail(comment) {
    $testGroup.append('<div class="assert failed">' + comment + '</div>')
    failed++; total++;
}

function pass(comment) {
    $testGroup.append('<div class="assert passed">' + comment + '</div>')
    total++;
}

function assertTrue(comment) {
     for (var i = 1; i < arguments.length; i++) {
        if (arguments[i] !== true) {
            fail(comment + ". Expected true. (false)");
            return;
        }
    }
    pass(comment);
}

function assertFalse(comment) {
     for (var i = 1; i < arguments.length; i++) {
        if (arguments[i] !== false) {
            fail(comment + ". Expected false. (true)");
            return;
        }
    }
    pass(comment)
}

function assertEqual(comment, val1, val2) {
    if (val1 != val2) {
        fail(comment + ". Expected equal (" + val1 + " = " + val2 + ")");
    } else pass(comment)
}

function assertNotEqual(comment, val1, val2) {
    if (val1 == val2) {
        fail(comment + ". Expected not equal (" + val1 + " != " + val2 + ")");
    } else pass(comment)
}

function assertNull(comment) {
    for (var i = 1; i < arguments.length; i++) {
        if (typeof arguments[i] !== 'undefined') {
            fail(comment + ". Expected undefined. (" +arguments[i] + ")");
            return;
        }
    }
    pass(comment)
}

function assertNotNull(comment) {
    for (var i = 1; i < arguments.length; i++) {
        if (arguments[i] === undefined || arguments[i] === null) {
            fail(comment + ". Expected not null. (undefined)");
            return;
        }
    }
    pass(comment)
}


$(function($) {
    $('body').prepend($header);
    include("main.js");
    if ($testGroup) endTestGroup();
    if (parseInt($(".failedCount", $header).html()) > 0) {
        $($header).addClass("failed");
    } else {
        $($header).addClass("passed");
    }
    $('body').append('<div style="border-top:1px solid #ccc"></div>');
});
